iT邦幫忙

2021 iThome 鐵人賽

DAY 19
1
AI & Data

想到甚麼打甚麼系列 第 19

找LeetCode上簡單的題目來撐過30天啦(DAY19)

  • 分享至 

  • xImage
  •  

**題號:86 標題:Partition List 難度:Medium

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.

Example 1:

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]

Example 2:
Input: head = [2,1], x = 2
Output: [1,2]

Constraints:
• The number of nodes in the list is in the range [0, 200].
• -100 <= Node.val <= 100
• -200 <= x <= 200

我的程式碼

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        if(head==null){
            return head;
        }
        ListNode temps = new ListNode(); //previous small
        ListNode tempeb = new ListNode(); //previous equal big
        ListNode small = new ListNode(); // str small 
        ListNode eqbi = new ListNode(); // str equal big
        ListNode Node_index = new ListNode(); //str
        ListNode ebst = new ListNode(-101); //head equal big
        ListNode sst = new ListNode(-101);//head small
        //root = head;

        Node_index = head;
        int chs=0,cheb=0;
        while(Node_index != null){
            if(Node_index.val<x){
                if(chs == 0 ){
                    small = Node_index;
                    sst =small;
                    temps = sst;
                    System.out.println("small: "+ Node_index.val);
                    Node_index = Node_index.next; 
                    chs++;
                }else{
                    temps.next = Node_index; 
                    small  = small.next;
                    temps = small;
                    System.out.println("small: "+ Node_index.val); 
                    Node_index = Node_index.next;
                }                 
            }else{
                if(cheb == 0 ){
                    eqbi = Node_index;
                    ebst = eqbi;
                    tempeb = ebst;
                    cheb++;
                    System.out.println("eqbi: "+ Node_index.val);
                    Node_index = Node_index.next;
                }else{
                    tempeb.next = Node_index; 
                    eqbi = eqbi.next;
                    tempeb = eqbi;
                    System.out.println("eqbi: "+ Node_index.val);
                    Node_index = Node_index.next;
                }
            }
        }
        eqbi.next = null;
        small.next = null;       
//         while(ebst != null){
//             System.out.println("ebst: "+ ebst.val);
//             ebst = ebst.next;
//         }
        
//         while(sst != null){
//             System.out.println("sst: "+ sst.val);
//             sst = sst.next;
//         }
        
            
        if(sst.val == -101){
            return ebst;
        }else if(ebst.val == -101){
            return sst;
        }else{
            temps.next = ebst;
            return sst;
        }
    }
}

DAY19心得
心態炸裂,不想打字


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY18)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY20)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言